var token;
var app = angular.module('app', [
        'ionic',
        // 'ionic-citydata',
        'ionic-citypicker',
        'ngStorage',
        'ngCookies',
        'app.config',
        'app.controllers',
        'app.directive',
        'app.services',
        'app.factory',
        'app.FuziController',
        'app.MemberController',
        'app.GoodsController',
        'app.OrderController',
        'app.VipController',
        'app.StorepayController',
        'app.HuodongController',
        'ngFileUpload',
        'base64',

])

    .run(function($ionicPlatform,$state,$http,$window,$localStorage,API,$rootScope,WechatConfig,$location) {
        var url = $location.absUrl();
        if(url.indexOf('index.html') == -1){
            var new_url = 'http://' + $location.host() + '/index.html'+window.location.search+window.location.hash;
            //跳转
            $window.location.href = new_url;
        }else{
            var access_token = $localStorage.access_token || '';
            API.getToken(access_token,function(err,res){
                if(res.status == 302) {
                    $window.location.href = res.location;
                    $localStorage.userinfo = '';
                    $localStorage.access_token = '';
                }else{
                    if(!res.data.access_token || err){
                        $state.reload();
                    }else{
                        $localStorage.userinfo = res.data.userinfo;
                        $localStorage.access_token = res.data.access_token;
                        $rootScope.access_token = access_token;
                        shareConfig();
                    }
                }
            });

            var shareConfig = function(){
                API.share({
                    url : location.href.split('#')[0]
                },function(err,result){
                    var share = result.data.share;
                    wx.config(share);
                    wx.ready(function(){
                        $rootScope.share = true;
                        shareAll();
                    });
                    wx.error(function(res){
                        API.getNewShare(shareConfig);
                    });
                });
            };
            var shareAll = function(){
                var url = window.location.origin + window.location.pathname + '?parent=' + $localStorage.userinfo.uid + window.location.hash;
                wx.onMenuShareTimeline({
                    title: WechatConfig.title, // 分享标题
                    link: url, // 分享链接
                    imgUrl: WechatConfig.imgUrl, // 分享图标
                });
                wx.onMenuShareAppMessage({
                    title: WechatConfig.title, // 分享标题
                    desc: WechatConfig.desc, // 分享描述
                    link: url, // 分享链接
                    imgUrl: WechatConfig.imgUrl, // 分享图标
                });
            };
            $ionicPlatform.ready(function() {
                // Hide the accessory bar by default (remove this to show the accessory bar above the keyboard
                // for form inputs)
                if (window.cordova && window.cordova.plugins && window.cordova.plugins.Keyboard) {
                    cordova.plugins.Keyboard.hideKeyboardAccessoryBar(true);
                    cordova.plugins.Keyboard.disableScroll(true);
                }
                if (window.StatusBar) {
                    // org.apache.cordova.statusbar required
                    StatusBar.styleDefault();
                }
            });
        }
    })

    .config(function ($stateProvider,$urlRouterProvider,$ionicConfigProvider,$locationProvider) {
        // $ionicConfigProvider.templates.maxPrefetch(0);
        //mePageLoadingProvider.effect = 'Lateral Swipe';
        // $ionicConfigProvider.views.maxCache(0);
        //$locationProvider.html5Mode({
        //    enabled: true,
        //    requireBase: false
        //});
        $ionicConfigProvider.platform.ios.tabs.style('standard');
        $ionicConfigProvider.platform.ios.tabs.position('bottom');
        $ionicConfigProvider.platform.android.tabs.style('standard');
        $ionicConfigProvider.platform.android.tabs.position('standard');

        $ionicConfigProvider.platform.ios.navBar.alignTitle('center');
        $ionicConfigProvider.platform.android.navBar.alignTitle('left');

        $ionicConfigProvider.platform.ios.backButton.previousTitleText('').icon('ion-ios-arrow-thin-left');
        $ionicConfigProvider.platform.android.backButton.previousTitleText('').icon('ion-android-arrow-back');

        $ionicConfigProvider.platform.ios.views.transition('ios');
        $ionicConfigProvider.platform.android.views.transition('android');
        $urlRouterProvider.otherwise("/index");
        $urlRouterProvider.when('/order', '/order/index');
        $urlRouterProvider.when('/member', '/member/index');
        $urlRouterProvider.when('/vip', '/vip/index');
        $urlRouterProvider.when('/order/index', '/order/index/0');
        $stateProvider
            .state(
                'index',{
                    cache: false,
                    url : '/index',
                    templateUrl: 'tpl/index/index.html',
                    controller: 'MainController',
                }
            )
            .state(
                'member',{
                    cache: false,
                    url : '/member',
                    templateUrl: 'tpl/public/main.html',
                    controller: 'MemberController',
                }
            )
            .state(
                'member.index',{
                    cache: false,
                    url : '/index',
                    templateUrl: 'tpl/member/index.html',
                    controller: 'MemberIndexController',
                }
            )
            .state(
                'member.info',{
                    url : '/info',
                    templateUrl: 'tpl/member/info.html',
                    controller: 'MemberInfoController'
                }
            )
            .state(
                'member.editinfo',{
                    url : '/editinfo',
                    templateUrl: 'tpl/member/editinfo.html',
                    controller: 'MemberEditInfoController'
                }
            )
            .state(
                'member.place',{
                    url : '/place',
                    templateUrl: 'tpl/member/place.html',
                    controller: 'MemberPlaceController'
                }
            )
            .state(
                'member.qrcode',{
                    url : '/qrcode',
                    templateUrl: 'tpl/member/qrcode.html',
                    controller: 'MemberQrcodeController'
                }
            )
            .state(
                'member.qrcode_create',{
                    url : '/qrcode_create',
                    templateUrl: 'tpl/member/qrcode_create.html',
                    controller: 'MemberQrcodeCreateController'
                }
            )
            .state(
                'member.parent',{
                    url : '/parent',
                    templateUrl: 'tpl/member/parent.html',
                    controller: 'MemberParentController'
                }
            )
            .state(
                'member.person',{
                    url : '/person/:id',
                    templateUrl: 'tpl/member/person.html',
                    controller: 'MemberPersonController'
                }
            )
            .state(
                'member.ally',{
                    url : '/ally/:type',
                    templateUrl: 'tpl/member/ally.html',
                    controller: 'MemberAllyController'
                }
            )
            .state(
                'member.mobile',{
                    url : '/mobile',
                    templateUrl: 'tpl/member/mobile.html',
                    controller: 'MemberMobileController'
                }
            )
            .state(
                'member.withdraw',{
                    url : '/withdraw',
                    templateUrl: 'tpl/member/withdraw.html',
                    controller: 'MemberWithdrawController'
                }
            )
            .state(
                'order',{
                    url : '/order',
                    templateUrl: 'tpl/public/main.html',
                    controller: 'OrderController',
                }
            )
            .state(
                'order.index',{
                    url : '/index/:status',
                    templateUrl: 'tpl/order/index.html',
                    controller: 'OrderIndexController',
                }
            )
            .state(
                'order.info',{
                    url : '/info/:order_id',
                    templateUrl: 'tpl/order/info.html',
                    controller: 'OrderInfoController',
                }
            )
            .state(
                'order.submit',{
                    url : '/submit',
                    templateUrl: 'tpl/order/submit.html',
                    controller: 'OrderSubmitController',
                }
            )
            .state(
                'goods',{
                    url : '/goods',
                    templateUrl: 'tpl/public/main.html',
                    controller: 'GoodsController',
                }
            )
            .state(
                'goods.info',{
                    url : '/info/:goods_id',
                    templateUrl: 'tpl/goods/info.html',
                    controller: 'GoodsInfoController',
                }
            )
            .state(
                'goods.index',{
                    url : '/index/:cate_id',
                    templateUrl: 'tpl/goods/index.html',
                    controller: 'GoodsIndexController',
                }
            )
            .state(
                'goods.cart',{
                    url : '/cart',
                    templateUrl: 'tpl/goods/cart.html',
                    controller: 'GoodsCartController',
                }
            )
            .state(
                'vip',{
                    url : '/vip',
                    templateUrl: 'tpl/public/main.html',
                    controller: 'VipController',
                }
            )
            .state(
                'vip.index',{
                    url : '/index',
                    templateUrl: 'tpl/vip/index.html',
                    controller: 'VipIndexController',
                }
            )
            .state(
                'vip.pay',{
                    url : '/pay',
                    templateUrl: 'tpl/vip/pay.html',
                    controller: 'VipPayController',
                }
            )
            .state(
                'vip.cashpay',{
                    url : '/cashpay',
                    templateUrl: 'tpl/vip/cashpay.html',
                    controller: 'VipCashPayController',
                }
            )
            .state(
                'fuzi',{
                    url : '/fuzi',
                    templateUrl: 'tpl/public/main.html',
                    controller: 'FuziController',
                }
            )
            .state(
                'fuzi.index',{
                    url : '/index',
                    templateUrl: 'tpl/fuzi/index.html',
                    controller: 'FuziIndexController',
                }
            )
            .state(
                'fuzi.category',{
                    url : '/category',
                    templateUrl: 'tpl/fuzi/category.html',
                    controller: 'FuziCategoryController',
                }
            )

            .state(
                'search',{
                    url : '/search/:query',
                    templateUrl: 'tpl/public/search.html',
                    controller: 'SearchController',
                }
            )

            .state(
                'start',{
                    url : '/start',
                    templateUrl: 'tpl/index/start.html',
                    controller: 'StartController',
                }
            )

            .state(
                'pay',{
                    url : '/pay?:info',
                    templateUrl: 'tpl/public/pay.html',
                    controller: 'PayController',
                }
            )

            .state(
                'member.detail',{
                    url : '/detail',
                    templateUrl: 'tpl/member/detail.html',
                    controller: 'MemberDetailController',
                }
            )

            .state(
                'vip.detail',{
                    url : '/detail',
                    templateUrl: 'tpl/vip/detail.html',
                    controller: 'VipDetailController',
                }
            )

            .state(
                'member.xiaofei',{
                    url : '/xiaofei',
                    templateUrl: 'tpl/member/xiaofei.html',
                    controller: 'MemberXioafeiController',
                }
            )

            .state(
                'storepay',{
                    url : '/storepay',
                    templateUrl: 'tpl/public/main.html',
                    controller: 'StorepayController',
                }
            )

            .state(
                'storepay.index',{
                    url : '/index',
                    templateUrl: 'tpl/store/index.html',
                    controller: 'StorepayIndexController',
                }
            )

            .state(
                'storepay.maya',{
                    url : '/maya',
                    templateUrl: 'tpl/store/maya.html',
                    controller: 'StorepayMayaController',
                }
            )

            .state(
                'storepay.xy',{
                    url : '/xy',
                    templateUrl: 'tpl/store/xy.html',
                    controller: 'StorepayXyController',
                }
            )

            .state(
                'storepay.pay',{
                    url : '/pay?:id',
                    templateUrl: 'tpl/store/pay.html',
                    controller: 'StorepayPayController',
                }
            )
            .state(
                'bdsy',{
                    url : '/bdsy',
                    templateUrl: 'tpl/bdsy/index.html',
                    controller: 'BdsyController',
                }
            )
            .state(
                'huodong',{
                    url : '/huodong',
                    templateUrl: 'tpl/huodong/index.html',
                    controller: 'HuodongController',
                }
            )
            .state(
                'huodongshow',{
                    url : '/huodongshow',
                    templateUrl: 'tpl/huodong/show.html',
                    controller: 'HuodongShowController',
                }
            )
            .state(
                'member.coupon',{
                    url : '/coupon/:status',
                    templateUrl: 'tpl/huodong/coupon.html',
                    controller: 'CouponController',
                }
            )

    });